home *** CD-ROM | disk | FTP | other *** search
/ Champak 29 / Volume 29 - JOGO DISK .iso / Games / jungle_adventure.swf / scripts / __Packages / WalkingObject.as < prev   
Text File  |  2006-11-29  |  6KB  |  195 lines

  1. class WalkingObject extends SSObject
  2. {
  3.    var classID = SSGlobal.CLSID_MOBILEOBJECT;
  4.    var collisionMask = SSGlobal.CLSID_SHAPE | SSGlobal.CLSID_MAINCHAR;
  5.    var assetID = "WalkingObject";
  6.    var gravity = SSGlobal.GRAVITY;
  7.    var walkLeft = true;
  8.    var healthValue = -0.2;
  9.    var speed = 50;
  10.    var immobilized = 0;
  11.    var immobilizeTime = 5;
  12.    var strength = 250;
  13.    var maxWalkableAngle = 0.4;
  14.    var collision = false;
  15.    var editor_isItem = true;
  16.    var editor_name = "WalkingObject";
  17.    var editor_args_names = ["walkLeft"];
  18.    var editor_args_values = [WalkingObject.prototype.walkLeft];
  19.    var editor_args_types = ["boolean"];
  20.    var editor_args_options = [null];
  21.    var editor_args_descriptions = [""];
  22.    var editor_args_mode = [0];
  23.    var editor_args_component = ["CheckBox"];
  24.    function WalkingObject()
  25.    {
  26.       super();
  27.    }
  28.    function onAddToScene()
  29.    {
  30.       this.getUpdates();
  31.       if(this.immobilized)
  32.       {
  33.          this.immobilized = Math.max(this.immobilized - (this.world.time - this.exitedSceneAt),0);
  34.       }
  35.       if(this.immobilized)
  36.       {
  37.          this.target.gotoAndStop(this.currentAnimation);
  38.       }
  39.       else
  40.       {
  41.          this.resetAnimation();
  42.       }
  43.    }
  44.    function onRemoveFromScene()
  45.    {
  46.       this.cancelUpdates();
  47.       this.exitedSceneAt = this.world.time;
  48.    }
  49.    function immobilize()
  50.    {
  51.       this.velocity.y = -300;
  52.       this.velocity.x = 0;
  53.       this.contact = null;
  54.       GameSound.playSound("enemyhit");
  55.       this.setAnimation("Immobile" + (!this.walkLeft ? "Right" : "Left"));
  56.       this.immobilized = this.immobilizeTime;
  57.    }
  58.    function setAnimation(anim)
  59.    {
  60.       this.target.gotoAndStop(this.currentAnimation = anim);
  61.    }
  62.    function resetAnimation()
  63.    {
  64.       this.setAnimation("Walk" + (!this.walkLeft ? "Right" : "Left"));
  65.    }
  66.    function onCollision(obj)
  67.    {
  68.       if((obj.classID & 4294901760) == SSGlobal.CLSID_MAINCHAR)
  69.       {
  70.          if(this.immobilized)
  71.          {
  72.             return undefined;
  73.          }
  74.          if(obj.classID == SSGlobal.CLSID_VEHICLE || obj.shield)
  75.          {
  76.             if(obj.active)
  77.             {
  78.                this.immobilize();
  79.             }
  80.             return undefined;
  81.          }
  82.          if(!obj.active)
  83.          {
  84.             return undefined;
  85.          }
  86.          if(!obj.jumpTime && obj.y < this.y)
  87.          {
  88.             this.immobilize();
  89.             obj.velocity.x *= 1.1;
  90.             obj.velocity.y *= -0.5;
  91.          }
  92.          else if(!obj.immune)
  93.          {
  94.             var _loc3_ = undefined;
  95.             obj.shiftHealth(this.healthValue,this);
  96.             if(this.walkLeft && obj.x < this.x || !this.walkLeft && obj.x > this.x)
  97.             {
  98.                this.flip();
  99.             }
  100.          }
  101.          return true;
  102.       }
  103.    }
  104.    function checkIfOnEdge(e)
  105.    {
  106.       var _loc8_ = e.a;
  107.       var _loc2_ = e.normal;
  108.       var _loc3_ = this.contact.direction;
  109.       var _loc5_ = this;
  110.       var _loc6_ = (_loc2_.x * (_loc5_.x - _loc2_.x * this.radius) + _loc2_.y * (_loc5_.y - _loc2_.y * this.radius) - (_loc2_.x * (_loc8_.x + this.contact.shape.x) + _loc2_.y * (_loc8_.y + this.contact.shape.y))) / (- (_loc2_.x * _loc3_.x + _loc2_.y * _loc3_.y));
  111.       if(_loc6_ * (!this.walkLeft ? 1 : -1) < ((e.props & 3) != 3 ? this.radius * 2 : 0))
  112.       {
  113.          if((e.props & 3) == 3)
  114.          {
  115.             var _loc7_ = Math.abs(_loc6_);
  116.             this.moveBy(_loc7_ * _loc3_.x,_loc7_ * _loc3_.y,0);
  117.             this.contact = e;
  118.          }
  119.          else
  120.          {
  121.             this.flip();
  122.          }
  123.          this.target._rotation = this.angle = Math.atan2(this.contact.normal.x,- this.contact.normal.y) / 3.141592653589793 * 180;
  124.       }
  125.    }
  126.    function flip()
  127.    {
  128.       this.walkLeft = !this.walkLeft;
  129.       this.resetAnimation();
  130.    }
  131.    function update(elapsed)
  132.    {
  133.       if(this.immobilized)
  134.       {
  135.          if(!(this.immobilized = Math.max(0,this.immobilized - elapsed)))
  136.          {
  137.             this.contact = null;
  138.             this.velocity.y = -200;
  139.             this.setAnimation("restore" + (!this.walkLeft ? "Right" : "Left"));
  140.          }
  141.       }
  142.       if(this.contact)
  143.       {
  144.          if(this.immobilized)
  145.          {
  146.             this.velocity.x = this.velocity.y = 0;
  147.          }
  148.          else
  149.          {
  150.             this.velocity.x = this.contact.direction.x * this.speed * (!this.walkLeft ? 1 : -1);
  151.             this.velocity.y = this.contact.direction.y * this.speed * (!this.walkLeft ? 1 : -1);
  152.          }
  153.          this.checkIfOnEdge(!this.walkLeft ? this.contact.next : this.contact.previous);
  154.       }
  155.       else
  156.       {
  157.          this.velocity.y += this.gravity * elapsed;
  158.       }
  159.       this.collision = false;
  160.       this.checkCollisions(elapsed);
  161.       if(!this.collision)
  162.       {
  163.          this.moveBy(this.velocity.x * elapsed,this.velocity.y * elapsed,0);
  164.       }
  165.    }
  166.    function setContact(edge)
  167.    {
  168.       this.contact = edge;
  169.    }
  170.    function checkCollision(obj)
  171.    {
  172.       var _loc2_ = undefined;
  173.       switch(obj.classID & 4294901760)
  174.       {
  175.          case SSGlobal.CLSID_SHAPE:
  176.             if(!this.contact && (_loc2_ = SSCollision.sweepSphereToStaticShape(this,obj)))
  177.             {
  178.                var _loc4_ = this.velocity.__get__length();
  179.                var _loc5_ = this.motionDir.dot(_loc2_.normal);
  180.                this.setContact(_loc2_.edge);
  181.                this.x = _loc2_.point.x;
  182.                this.y = _loc2_.point.y;
  183.                this.collision = true;
  184.             }
  185.             break;
  186.          case SSGlobal.CLSID_MAINCHAR:
  187.             if(SSCollision.sweepSphereToSphere(this,obj,true))
  188.             {
  189.                return this.onCollision(obj);
  190.             }
  191.             break;
  192.       }
  193.    }
  194. }
  195.